package com.yuncheng.spcyApi.selectXzxkApi.service;

import com.alibaba.fastjson.JSONObject;
import com.yuncheng.spcyApi.selectXzxkApi.vo.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.yuncheng.spcyApi.mapper.XzXkMapper;
import com.yuncheng.spcyApi.selectXzxkApi.constant.XzXkConstant;
import com.yuncheng.spcyApi.selectXzxkApi.constant.XzXkEnumConstant;
import com.yuncheng.spcyApi.utils.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component(value = "ypzcXzxkService")
public class YpzcXzxkService {
    private static final Logger log = LoggerFactory.getLogger(YpzcXzxkService.class);

    @Resource
    @Lazy
    private XzXkMapper xzXkMapper;

    /**
     * sqsxbm (001001027) / 变更生产场地备案
     */
    public Map bgSccdBa(String sqsxid){
        Map resultMap = new HashMap();

        // 企业信息
        List<XzspSqsxXdrxxVo> qyList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyList)){
            resultMap.put("qyxx", qyList.get(0));
        }else {
            resultMap.put("qyxx", new JSONObject());
        }

        // 国产药品补充申请
        XzspGcypbcsqVo bcsqMap = xzXkMapper.ypzcGcypBcSq(sqsxid);
//        JSONObject bcsqObject = MapUtils.mapToObject(bcsqMap);
        resultMap.put("bcsq", bcsqMap);

        // 国产药品补充申请历次申请情况
        List<XzspGcypbcsqlcsqqkVo> lcsqqkList = xzXkMapper.ypzcGcypBcSqLcSqQk(sqsxid);
//        List<Map<String, Object>> lcsqqkListArr = MapUtils.listMapKeyByDelEmpty(lcsqqkList);
        resultMap.put("lcsqqkList", lcsqqkList);

        // 国产药品补充申请委托研究机构
        List<XzspGcypbcsqwtyjjgVo> wtyjjgList = xzXkMapper.ypzcGcypBcSqWtYjjg(sqsxid);
//        List<Map<String, Object>> wtyjjgListArr = MapUtils.listMapKeyByDelEmpty(wtyjjgList);
        resultMap.put("wtyjjgList", wtyjjgList);

        // 国产药品补充申请原辅料来源
        List<XzspGcypbcsqyfllyVo> yfllyList = xzXkMapper.ypzcGcypBcSqYflly(sqsxid);
//        List<Map<String, Object>> yfllyListArr = MapUtils.listMapKeyByDelEmpty(yfllyList);
        resultMap.put("yfllyList", yfllyList);

        // 国产药品补充申请 中药材标准
        List<XzspGcypbcsqzycbzVo> zycbzList = xzXkMapper.ypzcGcypBcSqZycbz(sqsxid);
//        List<Map<String, Object>> zycbzListArr = MapUtils.listMapKeyByDelEmpty(zycbzList);
        resultMap.put("zycbzList", zycbzList);

        return resultMap;
    }

    /**
     * sqsxbm (001001045) / 医疗机构制剂注册补充申请
     */
    public Map yljgZjzcBcsq(String sqsxid){
        Map resultMap = new HashMap();

        // 医疗机构制剂注册补充申请
        List<XzspYljgzjxxVo> yljgZjxxList = xzXkMapper.ypzcYljgZjxx(sqsxid);
        if (CollectionUtils.isNotEmpty(yljgZjxxList)){
            resultMap.put("yljgZjxx", yljgZjxxList.get(0));
        }else {
            resultMap.put("yljgZjxx", new JSONObject());
        }

        // 企业信息
        List<XzspSqsxXdrxxVo> qyxxList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyxxList)){
            for (XzspSqsxXdrxxVo xdrMap : qyxxList) {
                String qysx_id = xdrMap.getQysx_id();
                if (qysx_id.equals(XzXkEnumConstant.Xdrxx_QYSX_ID_1.getIndex())) {
                    // 委托方医疗机构信息
                    resultMap.put("qyxx", xdrMap);

                }else if (qysx_id.equals(XzXkEnumConstant.Xdrxx_QYSX_ID_7.getIndex())) {
                    // 接受委托医疗机构信息
                    resultMap.put("jsWtYljgxx", xdrMap);

                }else if (qysx_id.equals(XzXkEnumConstant.Xdrxx_QYSX_ID_15.getIndex())) {
                    // 接受委托药品生产单位信息
                    resultMap.put("jsWtQyxx", xdrMap);

                }
            }
        }else {
            resultMap.put("qyxx", new JSONObject());
            resultMap.put("jsWtYljgxx", new JSONObject());
            resultMap.put("jsWtQyxx", new JSONObject());

        }

        return resultMap;
    }

    /**
     * sqsxbm (001001005) / 修改制剂注册标准
     */
    public Map xgZjzcBz(String sqsxid){
        Map resultMap = new HashMap();

        // 医疗机构制剂注册补充申请
        List<XzspYljgzjxxVo> yljgZjxxList = xzXkMapper.ypzcYljgZjxx(sqsxid);
        if (CollectionUtils.isNotEmpty(yljgZjxxList)){
            resultMap.put("yljgZjxx", yljgZjxxList.get(0));
        }else {
            resultMap.put("yljgZjxx", new JSONObject());
        }

        // 企业信息
        List<XzspSqsxXdrxxVo> qyxxList = xzXkMapper.selectQyxxList(sqsxid);
        if (CollectionUtils.isNotEmpty(qyxxList)){
            for (XzspSqsxXdrxxVo xdrMap : qyxxList) {
                String qysx_id = xdrMap.getQysx_id();
                if (qysx_id.equals(XzXkEnumConstant.Xdrxx_QYSX_ID_1.getIndex())) {
                    // 委托方医疗机构信息
                    resultMap.put("qyxx", xdrMap);

                }else if (qysx_id.equals(XzXkEnumConstant.Xdrxx_QYSX_ID_7.getIndex())) {
                    // 接受委托医疗机构信息
                    resultMap.put("jsWtYljgxx", xdrMap);

                }else if (qysx_id.equals(XzXkEnumConstant.Xdrxx_QYSX_ID_15.getIndex())) {
                    // 接受委托药品生产单位信息
                    resultMap.put("jsWtQyxx", xdrMap);

                }
            }
        }else {
            resultMap.put("qyxx", new JSONObject());
            resultMap.put("jsWtYljgxx", new JSONObject());
            resultMap.put("jsWtQyxx", new JSONObject());

        }

        return resultMap;
    }

    /**
     * sqsxbm (001001002) / 增加制剂的功能主治审批 (待确认) 1
     */
    public Map zjZjdGnZjSp(String sqsxid){
        Map resultMap = new HashMap();


        return resultMap;
    }

    /**
     * sqsxbm (001001001) / 医疗机构制剂注册申请 (待确认) 1
     */
    public Map yljgZjzcSq(String sqsxid){
        Map resultMap = new HashMap();


        return resultMap;
    }

    /**
     * sqsxbm (001001033) / 中药品种保护初审 (待确认)
     */
    public Map zyPzbhCs(String sqsxid){
        Map resultMap = new HashMap();


        return resultMap;
    }

    /**
     * sqsxbm (001001018) / 医疗机构制剂再注册审批 (待确认)
     */
    public Map yljgZjZzcSp(String sqsxid){
        Map resultMap = new HashMap();


        return resultMap;
    }

    /**
     * sqsxbm (001001019) / 医疗机构制剂调剂使用审批 (待确认)
     */
    public Map yljgZjDjSySp(String sqsxid){
        Map resultMap = new HashMap();


        return resultMap;
    }

    /**
     * sqsxbm (001001050) / 医疗机构制剂补充申请审批-制剂技术转让 (待确认)
     */
    public Map yljgZjbcsqSpZjjsZy(String sqsxid){
        Map resultMap = new HashMap();


        return resultMap;
    }

    /**
     * 企业人员-主要人员 / 其他人员
     */
    public Map qyryListMap(String sqsxid){
        Map resultMap = new HashMap();

        // 企业人员-主要人员
        List<QyRyVo> qyryList = xzXkMapper.selectQyRyList(sqsxid, null, true);
        resultMap.put("primaryQyryList", qyryList);

        // 企业其他人员
        List<QyRyVo> qtQyryList = xzXkMapper.selectQyRyList(sqsxid, XzXkConstant.Fzrlx_Type_00, false);
        resultMap.put("qtQyryList", qtQyryList);

        return resultMap;
    }

    /**
     * 查询 - 历史表 - 历史记录 - 返回 -变更前，变更后的信息
     */
    public JSONObject lsxx(String sqsxid){
        String ccdx = "";
        Map<String, Object> lsbMap = xzXkMapper.searchLsb(sqsxid);
        if (lsbMap != null){
            Map<String, Object> lsxxMap = xzXkMapper.searchLsjl(lsbMap.get("ID").toString());
            if (lsxxMap != null){
                try {
                    ccdx =  MapUtils.ClobToString((Clob)lsxxMap.get("CCDX"));
                } catch (SQLException e) {
                    log.error("SQLException[]: {}", e.getMessage());
                } catch (IOException e) {
                    log.error("SQLException[]: {}", e.getMessage());
                }
            }
        }

        if (StringUtils.isNotBlank(ccdx)){
            JSONObject jsonObject = JSONObject.parseObject(ccdx);
            return jsonObject;
        }else {
            return new JSONObject();
        }
    }
}
